import { NAnchor, type AnchorProps, type AnchorInst } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export interface AnchorFactoryConstructorType {
  props?: AnchorProps;
  attrs?: HTMLAttributes;
}
export class AnchorFactory {
  private attrs: HTMLAttributes = {};
  private props: AnchorProps = {};
  private defaultSlot: BaseComponentType = null;
  private domRef: AnchorInst | null = null;

  constructor(data?: AnchorFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: AnchorProps) {
    this.props = props;
    return this;
  }

  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }

  scrollTo(href: string) {
    this.domRef?.scrollTo(href);
  }

  create() {
    return (
      <NAnchor
        ref={(el: HTMLElement) => {
          this.domRef = el as unknown as AnchorInst;
        }}
        {...this.attrs}
        {...this.props}
      >
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,
        }}
      </NAnchor>
    );
  }
}
